<?php

namespace app\admin\command;

use addons\shopro\model\UserWalletLog;
use app\admin\model\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\admin\model\ScoreFreezeLog;
use think\Db;

class Score extends Command
{
    protected function configure()
    {
        $this->setName('score')
            ->addArgument('name', Argument::OPTIONAL, "your name")
            ->addOption('city', null, Option::VALUE_REQUIRED, 'city name')
            ->setDescription('Say Hello');
    }

    protected function execute(Input $input, Output $output)
    {
        $name = trim($input->getArgument('name'));
        $name = $name ?: 'thinkphp';

        if ($input->hasOption('city')) {
            $city = PHP_EOL . 'From ' . $input->getOption('city');
        } else {
            $city = '';
        }
        if ($name == 'unfreeze_score') {
            //所有待释放的积分
            $freeze_list = ScoreFreezeLog::where(['status' => 1,'is_unfreeze'=>1])->select();
            if ($freeze_list) {
                foreach ($freeze_list as $k => &$v) {
                    if ($v->released_day == $v->freeze_day) {
                        Db::name('score_freeze_log')->where(['id' => $v->id])->update(['status' => 0]);
                    }
                    if ($v->done_unfreeze <= $v->unfreeze) {
                        $score_unfreeze_log = [
                            'user_id' => $v->user_id,
                            'freeze_log_id' => $v->id,
                            'unfreeze_score' => $v->days_unfreeze,
                            'day' => $v->released_day + 1,
                            'ctime' => time()
                        ];
                        Db::name('score_unfreeze_log')->insert($score_unfreeze_log);
                        //$user = Db::name('user')->where(['id'=>$v->user_id])->find();
                        //$user = User::get($v->user_id);
                        $user = \app\admin\model\shopro\user\User::get($v->user_id);
                        $u_level = 'level'.$v['level'].'_dj_score';
                        Db::name('user')->where(['id' => $v->user_id])->setDec($u_level, $v->days_unfreeze);//冻结的积分减少
                        //解冻的积分增加为正式的积分
                        Db::name('user')->where(['id' => $v->user_id])->setInc("score", $v->days_unfreeze);//冻结的积分减少后正式的积分增加
                        $before = $user['score'];
                        //积分变动记录
                        $user_score_log = [
                            'user_id' => $v->user_id,
                            'score' => $v->days_unfreeze,
                            'before' => $before,
                            'after' => bcadd($before, $v->days_unfreeze, 2),
                            'memo' => '每日积分解冻',
                            'createtime' => time()
                        ];
                        Db::name('user_score_log')->insert($user_score_log);
                        UserWalletLog::write($user, $v->days_unfreeze, $before, bcadd($before, $v->days_unfreeze, 2), "unfreeze", $v->id, "score", '每日积分解冻', []);

                        Db::name('score_freeze_log')->where(['id' => $v->id])->setInc('released_day', 1);
                        Db::name('score_freeze_log')->where(['id' => $v->id])->setInc('done_unfreeze', $v->days_unfreeze);
                    }
                }
            }
        }
        $output->writeln("Score," . $name . '!' . $city);
    }
}